package com.winit.cloudlink.rabbitmq.test;

public abstract class LikeMatcher<T> implements Matcher<T> {
    protected String partten;

    public LikeMatcher(String partten) {
        this.partten = partten;
    }

    @Override
    public boolean matches(T item) {
        if (partten.startsWith("%") && partten.endsWith("%")) {
            String matchWord = partten.substring(1, partten.length() - 1);
            return getValue(item).indexOf(matchWord) > 0;
        }
        if (partten.startsWith("%")) {
            String suffix = partten.substring(1);
            return getValue(item).endsWith(suffix);
        }
        if (partten.endsWith("%")) {
            String prefix = partten.substring(0, partten.length() - 1);
            return getValue(item).startsWith(prefix);
        }
        return partten.equals(getValue(item));
    }

    @Override
    public String getNotMatchReason(T item) {
        return String.format("Connection id '%s' does not match expected id '%s'.", getValue(item), partten);
    }

    @Override
    public String getMatchReason(T item) {
        return String.format("Connection id '%s' matches for link.", partten);
    }

    public String getPartten() {
        return partten;
    }

    protected abstract String getValue(T item);

}
